{
 "cells": [
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-11T05:49:14.196010Z",
     "start_time": "2025-11-11T05:49:13.104495Z"
    }
   },
   "cell_type": "code",
   "source": "import torch",
   "id": "1b0e5b818f50c79d",
   "outputs": [],
   "execution_count": 2
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-11T06:06:18.519570Z",
     "start_time": "2025-11-11T06:06:18.515051Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 加法运算\n",
    "# a = torch.rand(2,3)\n",
    "# b = torch.rand(2,3)\n",
    "\n",
    "\n",
    "a = torch.tensor([2,3])\n",
    "b = torch.tensor([4,5])\n",
    "print(a)\n",
    "print(b)\n",
    "\n",
    "print(a+b)\n",
    "print(a.add_(b))\n",
    "print(a)\n",
    "\n"
   ],
   "id": "initial_id",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor([2, 3])\n",
      "tensor([4, 5])\n",
      "tensor([6, 8])\n",
      "tensor([6, 8])\n",
      "tensor([6, 8])\n"
     ]
    }
   ],
   "execution_count": 12
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-11T06:08:02.222575Z",
     "start_time": "2025-11-11T06:08:02.218084Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 加法运算\n",
    "# a = torch.rand(2,3)\n",
    "# b = torch.rand(2,3)\n",
    "\n",
    "\n",
    "a = torch.tensor([2,3])\n",
    "b = torch.tensor([4,5])\n",
    "print(a-b)\n",
    "print(torch.sub(a,b))\n",
    "print(f\"非 in-place操作 {a}\" )\n",
    "print(a.sub_(b))\n",
    "print(f\"in-place操作 {a}\" )"
   ],
   "id": "482bcbb711ba4510",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor([-2, -2])\n",
      "tensor([-2, -2])\n",
      "非 in-place操作 tensor([2, 3])\n",
      "tensor([-2, -2])\n",
      "in-place操作 tensor([-2, -2])\n"
     ]
    }
   ],
   "execution_count": 19
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-11T06:09:57.334828Z",
     "start_time": "2025-11-11T06:09:57.331550Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 乘法运算\n",
    "# 哈达玛积\n",
    "print(a*b)\n",
    "print(torch.mul(a,b))\n"
   ],
   "id": "f1aff448f8029b71",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor([ -8, -10])\n",
      "tensor([ -8, -10])\n"
     ]
    }
   ],
   "execution_count": 23
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "a = torch.tensor([1,2], dtype=torch.float64)\n",
    "b = torch.tensor([2,3], dtype=torch.float64)\n",
    "\n",
    "print(a*b)\n"
   ],
   "id": "a4e772d46936ffe3",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor([2., 6.], dtype=torch.float64)\n"
     ]
    }
   ],
   "execution_count": 55
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "print(a/b)\n",
    "print(a.div(b))\n",
    "print(a.div_(b))"
   ],
   "id": "47fa2fe9440dff6c",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor([0.5000, 0.6667], dtype=torch.float64)\n",
      "tensor([0.5000, 0.6667], dtype=torch.float64)\n",
      "tensor([0.5000, 0.6667], dtype=torch.float64)\n"
     ]
    }
   ],
   "execution_count": 56
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-11T06:46:16.902871Z",
     "start_time": "2025-11-11T06:46:16.899008Z"
    }
   },
   "cell_type": "code",
   "source": [
    "a1 = torch.ones(2,3)\n",
    "a2 = torch.ones(3,3)\n",
    "\n",
    "print(a1.mm(a2))\n",
    "print(a1.matmul(a2))\n",
    "print(\">>>>>>>>>>>>>\")\n",
    "print(a1)\n",
    "\n",
    "print(a2)"
   ],
   "id": "89d7c90214456f20",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor([[3., 3., 3.],\n",
      "        [3., 3., 3.]])\n",
      "tensor([[3., 3., 3.],\n",
      "        [3., 3., 3.]])\n",
      ">>>>>>>>>>>>>\n",
      "tensor([[1., 1., 1.],\n",
      "        [1., 1., 1.]])\n",
      "tensor([[1., 1., 1.],\n",
      "        [1., 1., 1.],\n",
      "        [1., 1., 1.]])\n"
     ]
    }
   ],
   "execution_count": 64
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-11T06:53:25.751118Z",
     "start_time": "2025-11-11T06:53:25.747096Z"
    }
   },
   "cell_type": "code",
   "source": [
    "import torch\n",
    "# dim > 2， 大于2的维度必须是一样的，最后2个维度， 列和行必须一致。\n",
    "a1 = torch.ones(1,2,2,3)\n",
    "a2 = torch.ones(1,2,3,3)\n",
    "\n",
    "print(a1)\n",
    "print(a1)\n",
    "print(\">>>>>>>>>>>>>\")\n",
    "print(torch.matmul(a1,a2))\n"
   ],
   "id": "4612a4611709bb27",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor([[[[1., 1., 1.],\n",
      "          [1., 1., 1.]],\n",
      "\n",
      "         [[1., 1., 1.],\n",
      "          [1., 1., 1.]]]])\n",
      "tensor([[[[1., 1., 1.],\n",
      "          [1., 1., 1.]],\n",
      "\n",
      "         [[1., 1., 1.],\n",
      "          [1., 1., 1.]]]])\n",
      ">>>>>>>>>>>>>\n",
      "tensor([[[[3., 3., 3.],\n",
      "          [3., 3., 3.]],\n",
      "\n",
      "         [[3., 3., 3.],\n",
      "          [3., 3., 3.]]]])\n"
     ]
    }
   ],
   "execution_count": 72
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "# 指数运算\n",
    "\n",
    "import torch\n",
    "\n",
    "a = torch.tensor([1,2])\n",
    "print(torch.pow(a,3))\n",
    "print(a**3)\n",
    "print(a.pow_(3))"
   ],
   "id": "f1ca275cc667e170",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor([1, 8])\n",
      "tensor([1, 8])\n",
      "tensor([1, 8])\n"
     ]
    }
   ],
   "execution_count": 79
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-11T06:57:33.801162Z",
     "start_time": "2025-11-11T06:57:33.760339Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 自然常熟 exp\n",
    "\n",
    "a = torch.tensor([1,2], dtype=torch.float32)\n",
    "print(a.type())\n",
    "print(torch.exp(a))\n",
    "print(torch.exp_(a))\n",
    "print(a.exp())\n",
    "print(a.exp_())"
   ],
   "id": "1454cd3a85722b2b",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "torch.FloatTensor\n",
      "tensor([2.7183, 7.3891])\n",
      "tensor([2.7183, 7.3891])\n",
      "tensor([  15.1543, 1618.1781])\n",
      "tensor([  15.1543, 1618.1781])\n"
     ]
    }
   ],
   "execution_count": 83
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "# 对数运算\n",
    "a = torch.tensor([1000000000000000,2], dtype=torch.float32)\n",
    "\n",
    "print(torch.log(a))\n",
    "print(torch.log_(a))"
   ],
   "id": "6d4cbeb870de3495",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor([34.5388,  0.6931])\n",
      "tensor([34.5388,  0.6931])\n"
     ]
    }
   ],
   "execution_count": 87
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
